#include "Thread.hpp"
#include <vector>
#include <iostream>

using namespace ThreadTest;

#define TICKETNUM 10000
#define THREADNUM 4
//模拟抢票，每个线程比作一个抢票机
int g_tickets = TICKETNUM;

//每个抢票机只要还有余票就一直抢
void route(int& ticket)
{
    while (ticket > 0)
    {
        usleep(10000);
        cout << "get a ticket : " << ticket << endl;
        ticket--;
    }
}


int main()
{
    cout << "main : &tickets: " << &g_tickets << endl; 
    vector<Thread<int>> threads;
    //创建线程
    for (int i = 0; i < THREADNUM; i++)
    {
        string thread_name = "Thread-" + to_string(i + 1);
        threads.emplace_back(route, g_tickets, thread_name);
    }

    //启动线程
    for (auto& thread : threads)
    {
        thread.Start();
    }

    //等待一批线程
    for (auto& thread : threads)
    {
        thread.Join();
        cout << thread.name() << " is done" << endl;
    }
    return 0;
}